package org.miyuki4j.cfgtool;

import java.util.ArrayList;
import java.util.List;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/26/星期六 16:45
 * @Version 1.0
 */
public final class Commands {

    private static List<Command> init(String[] args) throws Exception {
        List<Command> commands = new ArrayList<>();
        for (String arg : args) {
            for (Command.CommandEnum value : Command.CommandEnum.values()) {
                if (value.match(arg)) {
                    Command command = value.create();
                    command.init(args);
                    commands.add(command);
                }
            }
        }
        return commands;
    }

    private static void execute(List<Command> commands) throws Exception {
        for (Command command : commands) {
            command.execute();
        }
    }

    private static void exit(List<Command> commands) {
        for (Command command : commands) {
            command.exit();
        }
    }

    public static void run(String... args) throws Exception {
        List<Command> commands = init(args);
        execute(commands);
        exit(commands);
    }
}
